﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawner : MonoBehaviour
{
    public List<GameObject> groundsp=new List<GameObject>();
    public List<GameObject> enemies=new List<GameObject>();
    float[] Y=new float[8]{-2.14f,-2.349182f,-2.349182f,-0.5642761f,-0.5642761f,-2.349182f,-2.349182f,-2.349182f};
    public float spawntime;
    public float enemyspawntime;
    private float countTime;
    private float countTime2;
    private float totaltime;
    private int size=2;
    private Vector3 spawnposition;
    void Update()
    {
        countTime+=Time.deltaTime;
        countTime2+=Time.deltaTime;
        totaltime+=Time.deltaTime;
        HardLevel();
        SpawnGround();
        SpawnEnemies();
    }
    public void SpawnGround(){
        spawnposition=new Vector3(transform.position.x,1.7f,-1.413116f);
        if(countTime>=spawntime){
            Instantiate(groundsp[0],spawnposition,Quaternion.identity);
            countTime=0;
        }
    }
    public void SpawnEnemies(){
        int index=Random.Range(0,size);
        spawnposition=new Vector3(transform.position.x,Y[index],-3f);
        if(countTime2>=enemyspawntime){
            Instantiate(enemies[index],spawnposition,Quaternion.identity);
            countTime2=0;
            if(index>=3&&index<5)countTime2-=1;
            else if(index>=5)countTime2++;
        }
    }
    public void HardLevel(){
        if(totaltime>20)size=8;
    }
}
